package com.czwj.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.czwj.dao.DailyReportMapper;
import com.czwj.dao.PlanDailyMapper;
import com.czwj.dao.PlanMainMapper;
import com.czwj.entities.DailyReport;
import com.czwj.entities.PlanDaily;
import com.czwj.entities.PlanMain;
import com.czwj.service.DailyReportService;

@Service
public class DailyReportServiceImpl implements DailyReportService {

	@Resource
	DailyReportMapper mapper;
	@Resource
	PlanMainMapper mainmapper;
	@Resource
	PlanDailyMapper dailymapper;

	@Override
	public List<String> existreportdate(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.mapper.existreportdate(map);
	}

	@Transactional
	@Override
	public int editdailyreport(List<DailyReport> list, Integer userid, String ip,Integer projectitemid) {
		int count = 0;
		// TODO Auto-generated method stub
		for (DailyReport report : list) {
			double finish = 0;
			PlanDaily daily = dailymapper.selectByPrimaryKey(report.getPlandailyid());
			if(report.getId() ==-1){
				report.setId(null);
				report.setCreaterid(userid);
				report.setCreatedate(new Date());
				report.setCreateip(ip);
				count += mapper.insertSelective(report);
				if(report.getPlandailyid()!=null){
					finish+= new BigDecimal(report.getPercent()).multiply(new BigDecimal(daily.getPercent())).doubleValue()/100;
					PlanMain main = daily.getPlanMain();
					main.setFinishpercent(main.getFinishpercent()+finish);
					if(main.getFinishpercent()==100){
						main.setStatus(5);
					}
					mainmapper.updateByPrimaryKeySelective(main);
				}
			}else{
				count += mapper.updateByPrimaryKeySelective(report);
			}
		}
		return count;
	}

	/** 
	* 
	* @param map
	* @return
	* @see com.czwj.service.DailyReportService#search(java.util.Map)
	*/
	@Override
	public List<DailyReport> search(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.search(map);
	}

}
